package com.system.struts2.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.base.service.impl.ServiceImpl;
import com.opensymphony.xwork2.ActionSupport;
import com.system.pojo.Dept;
import com.system.pojo.Noticeboard;
import com.system.pojo.Person;
import com.system.service.IDeptService;
import com.system.service.INoticeboardService;
import com.system.service.IPersonService;
import com.system.struts2.util.PersonInfo;
import com.system.struts2.util.PersonUtil;

public class DeptAction extends ActionSupport {

	private boolean flag = false;
	private Dept dept;
	private IDeptService<Dept> deptSer;
	public List<Dept> deptList = new ArrayList<Dept>();
	private Person person;
	private IPersonService<Person> personSer;
	private Noticeboard noticeborad;
	private INoticeboardService noticeboardSer;
	public List<Noticeboard> noticeboradList = new ArrayList<Noticeboard>();
	HttpServletRequest request = null;
	HttpServletResponse response = null;

	public String addNoticBoard() {
		if (noticeborad.getContent() == null
				|| noticeborad.getContent().equals("")) {
			addActionMessage("公告内容不能为空！");
		} else {
			request = ServletActionContext.getRequest();
			response = ServletActionContext.getResponse();
			PersonInfo personInfo = PersonUtil.getPersonInfo(request, response);
			person = personSer.find(Person.class, personInfo.getId());
			noticeborad.setDateCreated(new Date());
			noticeborad.setPerson(person);
			flag = noticeboardSer.create(noticeborad);
			if (flag = true) {
				addActionMessage("公告发布成功！");
			} else {
				addActionMessage("公告更新成功！");
			}
		}
		return "noticeBoard";
	}

	public String initAdd() {
		deptList = deptSer.list("From Dept ", 0, Integer.MAX_VALUE, null);
		return "initAdd";
	}
	
	public String save()
	{
		Dept deptTemp = deptSer.find(Dept.class, dept.getUpDept());
		if(deptTemp.getTopDept()!=0){
			deptTemp = deptSer.find(Dept.class, deptTemp.getUpDept());
		}
		while(deptTemp.getTopDept()!=0){
			deptTemp = deptSer.find(Dept.class, deptTemp.getUpDept());
		}
		
		dept.setTopDept(deptTemp.getId());
		dept.setDeleted(false);
		deptSer.create(dept);
		return "dept";
	}
	public String list()
	{
		deptList = deptSer.list("from Dept ", 0, Integer.MAX_VALUE, null);
		return "dept";
	}

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

	public Dept getDept() {
		return dept;
	}

	public void setDept(Dept dept) {
		this.dept = dept;
	}

	public IDeptService<Dept> getDeptSer() {
		return deptSer;
	}

	public void setDeptSer(IDeptService<Dept> deptSer) {
		this.deptSer = deptSer;
	}

	public List<Dept> getDeptList() {
		return deptList;
	}

	public void setDeptList(List<Dept> deptList) {
		this.deptList = deptList;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public IPersonService<Person> getPersonSer() {
		return personSer;
	}

	public void setPersonSer(IPersonService<Person> personSer) {
		this.personSer = personSer;
	}

	public Noticeboard getNoticeborad() {
		return noticeborad;
	}

	public void setNoticeborad(Noticeboard noticeborad) {
		this.noticeborad = noticeborad;
	}

	public INoticeboardService getNoticeboardSer() {
		return noticeboardSer;
	}

	public void setNoticeboardSer(INoticeboardService noticeboardSer) {
		this.noticeboardSer = noticeboardSer;
	}

	public List<Noticeboard> getNoticeboradList() {
		return noticeboradList;
	}

	public void setNoticeboradList(List<Noticeboard> noticeboradList) {
		this.noticeboradList = noticeboradList;
	}

}
